﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;
using System.Web.Security;

namespace CoolCode.Web.UI.Views.Admin
{
    public partial class UserRoles :AuthPage
    {
        //private IMenuService _MenuService;
        //public IMenuService MenuService
        //{
        //    get
        //    {
        //        if (_MenuService == null)
        //        {
        //            _MenuService = ServiceFactory.Produce<IMenuService>();
        //        }
        //        return _MenuService;
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
        }

        protected void handleEvents()
        {
            txtRole.TextChanged += new EventHandler(txtRole_TextChanged);
            lnkSave.Click += new EventHandler(lnkSave_Click);
            lnkDelete.Click += new EventHandler(lnkDelete_Click);
            new string[] { "chkRead", "chkWrite", "chkDelete", "chkExport" }
                .ForEach(c => gvMaster.HandleSelectAll(c + "SelectAll", c));
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            string role = txtRole.Text;
            if (string.IsNullOrEmpty(role))
            {
                udpHeader.Alert("请先选择" + lblUser.Text);
                return;
            }
            if (role.Equals(Role.DefaultUserRole, StringComparison.InvariantCultureIgnoreCase  ) || 
                role.Equals( Role.DefaultAdminRole, StringComparison.InvariantCultureIgnoreCase))
            {
                udpHeader.Alert(string.Format("{0} 或 {1} 角色是由系统创建，不能删除！ ",
                    Role.DefaultUserRole, Role.DefaultAdminRole));
                return;
            }
            if (Roles.RoleExists(role))
            {
                string[] users = Roles.GetUsersInRole(role);
                if (users.Length > 0)
                {
                    udpHeader.Alert(string.Format("此角色已经被以下用户使用：{0}，请先修改这些用户的角色。", users.Join(",")));
                    return;
                }
                Roles.DeleteRole(role);
            }
            this.MenuService.DeleteMenuInRole(role, null);
            txtRole.Text = string.Empty;
            udpHeader.Alert("操作成功！");
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            List<RoleView> list = new List<RoleView>();
            foreach (GridViewRow gvr in gvMaster.Rows)
            {
                RoleView entity = new RoleView
                {
                    MenuID = gvMaster.GetKey<int>(gvr.RowIndex),
                    BelongTo = txtRole.Text,
                    CanRead = gvr.IsSelected("chkRead"),
                    CanWrite = gvr.IsSelected("chkWrite"),
                    CanDelete = gvr.IsSelected("chkDelete"),
                    CanExport = gvr.IsSelected("chkExport"),
                };
                list.Add(entity);
            }
            this.MenuService.SaveMenuInRole(list, null);
            if (!Roles.RoleExists(txtRole.Text))
            {
                Roles.CreateRole(txtRole.Text);
            }
            udpHeader.Alert("操作成功！");
            txtRole.Text = string.Empty;
            BindData();
            udpRole.Update();
            udpHeader.Update();
        }

        protected void txtRole_TextChanged(object sender, EventArgs e)
        {
            BindData();
            udpHeader.Update();
        }

        private void BindData()
        {
            string user = txtRole.Text;
            gvMaster.DataSource = this.MenuService.ListMenuInRole(user);
            gvMaster.DataBind();
        }


    }
}
